﻿using MicroCloud.Utils;

namespace Corporation.Project.Hangfire
{
    /// <summary>
    /// Hangfire作业运行器
    /// </summary>
    [Dependency(ServiceLifetime.Singleton)]
    public class HangfireJobRunner : IHangfireJobRunner
    {
        /// <summary>
        /// 启动作业运行器
        /// </summary>
        public void Start()
        {
            //BackgroundJob.Enqueue<UserManager<User>>(m => m.FindByIdAsync("2"));
            //string jobId = BackgroundJob.Schedule<UserManager<User>>(m => m.FindByIdAsync("2"), TimeSpan.FromMinutes(2));
            //BackgroundJob.ContinueJobWith<TestHangfireJob>(jobId, m => m.GetUserCount());

            //队列任务
            BackgroundJob.Enqueue(() => Console.WriteLine($@"队列任务"));

            //延时任务
            var jobId = BackgroundJob.Schedule(() => Console.WriteLine($@"延时任务"), TimeSpan.FromMinutes(1));

            //延续性任务执行
            BackgroundJob.ContinueJobWith(jobId, () => Console.WriteLine($@"延续性任务执行"));

            //定时任务
            RecurringJob.AddOrUpdate("JobId1", () => Console.WriteLine($@"定时任务：每年的4月12号15点52分任意秒执行 没加时区 小时+8"), "* 52 15 12 4 *");   //每年的4月12号15点52分任意秒执行 没加时区 小时+8
            RecurringJob.AddOrUpdate("JobId2", () => Console.WriteLine($@"定时任务：每分钟执行一次"), Cron.Minutely);      //每分钟执行一次。
            RecurringJob.AddOrUpdate("JobId3", () => Console.WriteLine($@"定时任务：每天18点36分 当前时区"), Cron.Daily(18, 36), new RecurringJobOptions { TimeZone = TimeZoneInfo.Local });  //每天18点36分 当前时区

            //本机示例
            RecurringJob.AddOrUpdate<TestHangfireJob>("TestHangfireJob_Test", m => m.Test(), Cron.Minutely, new RecurringJobOptions { TimeZone = TimeZoneInfo.Local });
            //RecurringJob.AddOrUpdate<TestHangfireJob>("TestHangfireJob_TestAsync", m => m.TestAsync(), Cron.Minutely, new RecurringJobOptions { TimeZone = TimeZoneInfo.Local });
        }

    }

}